package com.chenfan.filestore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.filestore.dto.TaskQueryDTO;
import com.chenfan.filestore.model.BaseEntity;
import com.chenfan.filestore.model.BatchExportTemplate;
import com.chenfan.filestore.repository.TaskDao;
import com.chenfan.filestore.model.Task;
import com.chenfan.filestore.service.TaskService;
import com.chenfan.filestore.utils.LambdaQueryUtil;
import com.chenfan.infra.context.UserContextHolder;
import com.chenfan.infra.vo.UserVO;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.function.Function;

/**
 * 任务表(Task)表服务实现类
 *
 * @author Leo
 * @since 2023-04-27 11:46:13
 */
@Service("taskService")
public class TaskServiceImpl extends ServiceImpl<TaskDao, Task> implements TaskService {

    @Autowired
    private TaskDao taskDao;

    @Override
    public Page<Task> page(TaskQueryDTO query) {
        UserVO userVo = UserContextHolder.getUserVo();
        if (Objects.nonNull(userVo) && Objects.nonNull(userVo.getUserId())){
            query.setCreateBy(userVo.getUserId());
        }
        Page<Task> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<Task> queryWrapper = LambdaQueryUtil.newLambdaQuery();
        queryWrapper.eq(Objects.nonNull(query.getTaskId()), Task::getTaskId, query.getTaskId());
        queryWrapper.eq(Objects.nonNull(query.getSystemId()), Task::getSystemId, query.getSystemId());
        queryWrapper.like(Objects.nonNull(query.getTaskName()), Task::getTaskName, query.getTaskName());
        queryWrapper.eq(Objects.nonNull(query.getModuleId()), Task::getModuleId, query.getModuleId());
        queryWrapper.eq(Objects.nonNull(query.getStatus()), Task::getStatus, query.getStatus());
        queryWrapper.eq(Objects.nonNull(query.getCreateBy()), Task::getCreateBy, query.getCreateBy());
        queryWrapper.eq(Objects.nonNull(query.getTaskType()), Task::getTaskType, query.getTaskType());
        queryWrapper.eq(Task::getIsDelete,Task.DELETE_NO);
        queryWrapper.ge(Objects.nonNull(query.getCreateStartTime()), Task::getCreateDate, query.getCreateStartTime());
        queryWrapper.le(Objects.nonNull(query.getCreateEndTime()), Task::getCreateDate, query.getCreateEndTime());
        queryWrapper.orderByDesc(Task::getCreateDate);
        return this.page(page,queryWrapper);
    }

    @Override
    public void findByBatch(String sql,ResultHandler handler) {
        taskDao.findByBatch(sql,handler);
    }


    @Override
    public Long findByCount(String sql) {
        return taskDao.findByCount(sql);
    }

    @Override
    public List<Task> listBySize(Integer size) {
        LambdaQueryWrapper<Task> queryWrapper = LambdaQueryUtil.newLambdaQuery();
        queryWrapper.eq(Task::getStatus, Task.STATUS_TO_DEAL);
        queryWrapper.last(String.format("limit %s", size));
        return this.list(queryWrapper);
    }

    @Override
    public List<Task> listByExpireDays(Integer expireDays) {
        LambdaQueryWrapper<Task> queryWrapper = LambdaQueryUtil.newLambdaQuery();
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(expireDays);
        queryWrapper.le(BaseEntity::getCreateDate, localDateTime);
        return this.list(queryWrapper);
    }

    @Override
    public Task queryLasteast(Task query) {
        LambdaQueryWrapper<Task> queryWrapper = LambdaQueryUtil.newLambdaQuery();
        queryWrapper.eq(Task::getCreateBy, query.getCreateBy());
        queryWrapper.eq(Task::getTemplateId, query.getTemplateId());
        queryWrapper.eq(Objects.nonNull(query.isShowImage()),Task::isShowImage, query.isShowImage());
        queryWrapper.orderByDesc(Task::getTaskId);
        queryWrapper.last("limit 1");
        List<Task> list = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(list)) {
            return null;
        }else{
            return list.get(0);
        }
    }


}

